package cs2340.klondike.TaskFudger;

import java.sql.SQLException; 

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import db.DatabaseHelper;
import models.User;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * This class gets the information from the user and stores it.
 * @version 1.0
 */
public class NewUserActivity extends OrmLiteBaseActivity<DatabaseHelper>
{
	private Button registerButton;
	private Button cancelButton;
	private EditText nameField;
	private EditText emailField;
	private EditText handleField;
	private EditText passwordField;
	private EditText confirmField;
	private TextView nameMessage;
	private TextView emailMessage;
	private TextView handleMessage;
	private TextView passwordMessage;
	private TextView confirmMessage;
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.new_user);
	        
	        registerButton = (Button)findViewById(R.id.save);
	    	cancelButton = (Button)findViewById(R.id.cancel);
	    	nameField = (EditText)findViewById(R.id.name);
	    	emailField = (EditText)findViewById(R.id.email);
	    	handleField = (EditText)findViewById(R.id.handle);
	    	passwordField = (EditText)findViewById(R.id.password);
	    	confirmField = (EditText)findViewById(R.id.passwordConfirmation);
	    	nameMessage = (TextView)findViewById(R.id.nameLabel);
	    	emailMessage = (TextView)findViewById(R.id.emailLabel);
	    	handleMessage = (TextView)findViewById(R.id.handleLabel);
	    	passwordMessage = (TextView)findViewById(R.id.passwordLabel);
	    	confirmMessage = (TextView)findViewById(R.id.passwordConfirmationLabel);
	        
	        cancelButton.setOnClickListener(new Button.OnClickListener(){
	        	public void onClick(View v){
	        		Intent intent = new Intent(v.getContext(), UserLoginActivity.class); //UserLoginActivity
	        		startActivity(intent);
	        	}
	        });
	        registerButton.setOnClickListener(new Button.OnClickListener(){
	        	public void onClick(View v){
	        		boolean infoValid = true;
	        		
	        		if (nameField.getText().toString().length() <= 0){
	        			infoValid = false;
	        			nameMessage.setText("Name required");
	        			nameMessage.setTextColor(Color.RED);
	        		}
	        		else{
	        			nameMessage.setText("Full Name");
	        			nameMessage.setTextColor(Color.WHITE);
	        		}
	        		
	        		if (emailField.getText().toString().length() <= 0){
	        			infoValid = false;
	        			emailMessage.setText("Email required");
	        			emailMessage.setTextColor(Color.RED);
	        		}
	        		else{
	        			emailMessage.setText("Email");
	        			emailMessage.setTextColor(Color.WHITE);
	        		}

	        		if (handleField.getText().toString().length() <= 0){
	        			infoValid = false;
	        			handleMessage.setText("Handle required");
	        			handleMessage.setTextColor(Color.RED);
	        		}
	        		else{
	        			handleMessage.setText("Handle");
	        			handleMessage.setTextColor(Color.WHITE);
	        		}
	        		
	        		if (passwordField.getText().toString().length() <= 0){
	        			infoValid = false;
	        			passwordMessage.setText("Password required");
	        			passwordMessage.setTextColor(Color.RED);
	        		}
	        		else{
	        			passwordMessage.setText("Password");
	        			passwordMessage.setTextColor(Color.WHITE);
	        		}

	        		if (!passwordField.getText().toString().equals(confirmField.getText().toString())){
	        			infoValid = false;
	        			confirmMessage.setText("Passwords do not match");
	        			confirmMessage.setTextColor(Color.RED);
	        		}
	        		else if (passwordField.getText().toString().length() <= 0){
	        			infoValid = false;
	        			confirmMessage.setText("Invalid Password");
	        			confirmMessage.setTextColor(Color.RED);
	        		}
	        		else{
	        			confirmMessage.setText("Confirm");
	        			confirmMessage.setTextColor(Color.WHITE);
	        		}

	        		if (infoValid) {
	        			User newUser = new User();
	        			newUser.setHandle(handleField.getText().toString());
	        			newUser.setName(nameField.getText().toString());
	        			newUser.setEmail(emailField.getText().toString());
	        			newUser.setPassword(passwordField.getText().toString());
	        			try {
							newUser.create();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
	        			
	        			User.setCurrentUser(newUser);
	        			
	        			Intent intent = new Intent(v.getContext(), UserLoginActivity.class); //UserLoginActivity
	        			startActivity(intent);
	        		}
	        		else{
	        			passwordField.setText("");
	        			confirmField.setText("");
	        		}
	        	}
	        });
	 }
}